﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LPV_POS.mh_pos.mh_ban_hang
{
    public partial class frmDM_BAN_DAT_BAN_ChonMon : Form
    {
        public frmDM_BAN_DAT_BAN_ChonMon()
        {
            InitializeComponent();
            DataTable tb = new DataTable();
            tb.Columns.Add("OBJID");
            tb.Columns.Add("OBJNAME");
            grdDM_HH1.DataSource = tb;
        }


        private void frmDM_BAN_DAT_BAN_ChonMon_Load(object sender, EventArgs e)
        {
            DataTable tbDM_NHOMHH = new thuvien().SP_SelectDataTable("SP_EXECUTESQL",
                                                    new string[] { "@sSQL" },
                                                    new object[] { "SELECT dn.OBJID, dn.OBJNAME FROM DM_NHOMHH dn" }
                                                    );
            grdDM_NHOMHH.DataSource = tbDM_NHOMHH;
        }

        private void grvDM_NHOMHH_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (grvDM_NHOMHH.FocusedRowHandle >= 0)
            {
                DataRow dr = grvDM_NHOMHH.GetDataRow(grvDM_NHOMHH.FocusedRowHandle);
                DataTable tbDM_HH = new thuvien().SP_SelectDataTable("SP_EXECUTESQL",
                                                    new string[] { "@sSQL" },
                                                    new object[] { "SELECT dh.OBJID, dh.OBJNAME FROM DM_HH dh WHERE dh.MANHOMHH = '" + dr["OBJID"] + "' AND ISNULL(dh.OBJNAME,'') <> '' ORDER BY LTRIM(dh.OBJNAME)" }
                                                    );
                grdDM_HH.DataSource = tbDM_HH;
            }
        }


        private void btnNext_Click(object sender, EventArgs e)
        {
            if (grvDM_HH.FocusedRowHandle >= 0)
            {
                DataRow dr1 = grvDM_HH.GetDataRow(grvDM_HH.FocusedRowHandle);
                if (dr1 != null)
                { 
                    DataTable tbDM_HH = (DataTable)grdDM_HH1.DataSource;
                    if (tbDM_HH.Select("OBJID = '" + dr1["OBJID"] + "'").Length == 0)
                    {
                        DataRow dr2 = tbDM_HH.NewRow();
                        dr2["OBJID"] = dr1["OBJID"];
                        dr2["OBJNAME"] = dr1["OBJNAME"];
                        tbDM_HH.Rows.Add(dr2);
                        grdDM_HH1.DataSource = tbDM_HH;
                        grdDM_HH1.Refresh();
                    }
                }
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            int ihand = grvDM_HH1.FocusedRowHandle;
            grvDM_HH1.FocusedRowHandle = -1;
            grvDM_HH1.FocusedRowHandle = ihand;
            grvDM_HH1.SelectRow(grvDM_HH1.FocusedRowHandle);
            
            this.grvDM_HH1_FocusedRowChanged(null, null);
            if (grvDM_HH.FocusedRowHandle >= 0)
            {
                DataTable tbDM_HH = (DataTable)grdDM_HH1.DataSource;

                if (sMaHH_DangChon != "")
                {
                    for(int i=0; i<tbDM_HH.Rows.Count;i++)
                        if (tbDM_HH.Rows[i]["OBJID"] + "" == sMaHH_DangChon)
                        {
                            tbDM_HH.Rows.Remove(tbDM_HH.Rows[i]);
                            break;
                        }                    
                    grdDM_HH1.DataSource = tbDM_HH;
                    grdDM_HH1.Refresh();
                    grvDM_HH1.RefreshData();
                    this.grvDM_HH1_FocusedRowChanged(null, null);
                }
            }
        }

        
        string sMaHH_DangChon = "";
        private void grvDM_HH1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            sMaHH_DangChon = "";
            DataRow dr1 = grvDM_HH1.GetDataRow(grvDM_HH1.FocusedRowHandle);
            if (dr1 != null)
                sMaHH_DangChon = "" + dr1["OBJID"];
        }

        public DataTable ShowForm()
        {
            this.ShowDialog();
            return (DataTable)grdDM_HH1.DataSource;
        }
    }
}
